# -*- coding: utf-8 -*-

from django.template.response import TemplateResponse
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login ,logout
from django.views.decorators.cache import cache_page

#@cache_page(60 * 10) 
def home(request):    
    return TemplateResponse(request,'index.html',{'next':request.GET.get('next',''),})

#@cache_page(60 * 10) 
def user_guide(request):
    return TemplateResponse(request,'user_guide.html',{'app':'user_guide'})
                            
                            
def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')

def login_view(request):
    if request.method =='POST' :
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                
                # Redirect to a success page.                
                if request.POST.get('next',''):
                    return HttpResponseRedirect(request.POST['next'])
                else:
                    return HttpResponseRedirect('/')
            else:
                # Return a 'disabled account' error message
                pass
        else:
            # Return an 'invalid login' error message.
            pass
        
    return TemplateResponse(request,'login.html')
        
        